import { IGetAllAssetsReq, IGetAllAssetsRes, IUploadAssetsReq, ReqApiUrls } from "@/headers/admin/reqs.h";
import { IAdminStore } from "@/headers/admin/store.h";
import { Store, ActionContext } from "vuex";
import { ActionBase, registerAll } from "../common";
import {apiAxios as axios} from '@/utils/axios';
import { IAssetsModel } from "@/headers/admin/model.h";

export class GetAllAssetsAction extends ActionBase<IAdminStore,IAdminStore,IGetAllAssetsReq,IGetAllAssetsRes> {
    public async handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, payload: IGetAllAssetsReq): Promise<IGetAllAssetsRes> {
       const {data} = await axios.get<IGetAllAssetsRes>(ReqApiUrls.Assets,{
           params:payload
       })
       return data;
    }
}

export class UploadAssetsAction extends ActionBase<IAdminStore,IAdminStore,IUploadAssetsReq,IAssetsModel> {
    public async  handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, payload: IUploadAssetsReq): Promise<IAssetsModel> {
        const formData = new FormData();
        formData.append('file',payload.file);

        const {data} = await axios.post<IAssetsModel>(ReqApiUrls.Assets,formData,{
            headers:{
                'Content-Type':'multipart/form-data'
            },
        })
        return data;

    }
}

export function initAssetsAction(target:any) {
    registerAll(
        target,
        GetAllAssetsAction,
        UploadAssetsAction
    )
}